package com.huarui.receive.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.receive.dao.IReceiveItemDao;
import com.huarui.receive.model.ReceiveItem;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class ReceiveItemDaoImpl extends BaseDaoImpl implements IReceiveItemDao {

	@Override
	public ReceiveItem getReceiveItemByOrdersItemId(Integer ordersItemId) {
		
		return (ReceiveItem) getSqlMapClientTemplate().queryForObject(namespace+".getReceiveItemByOrdersItemId", ordersItemId);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,String>> getReceiveItemByReceiveId(Integer receiveId) {
		
		return getSqlMapClientTemplate().queryForList(namespace+".getReceiveItemByReceiveId",receiveId);
	}


	@Override
	public Integer batchDeleteItems(Map<String,Object> map) {		
		return getSqlMapClientTemplate().delete(namespace+".deleteByIds", map);		
	}


	@Override
	public void batchUpdateItems(final List<ReceiveItem> receiveItems) {
		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch = 0;
				for(ReceiveItem receiveItem:receiveItems){
					executor.update(namespace+".update",receiveItem);
					batch++;
					if(batch == 100){
						executor.executeBatch();
						batch = 0;
					}
				}
				 executor.executeBatch();
				 return null;
			}
		});
	
	}


	@Override
	public void batchInsertItems(final List<ReceiveItem> receiveItems) {
         this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch = 0;
				for(ReceiveItem receiveItem:receiveItems){
					executor.insert(namespace+".create", receiveItem);
					batch++;
					if(batch == 100){
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getReceiveItemByConditons(Map map) {
		return (List<Map>) getSqlMapClientTemplate().queryForList(namespace+".getReceiveItemByConditions", map);
	}


	@SuppressWarnings("unchecked")
	@Override
	public Integer getReceiveItemCountByConditions(Map map) {
	
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getReceiveItemCountByConditions", map);
	}

}
